unit mainform;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, XPMan, TestBase;

type
  TFormMain = class(TForm)
    pagecontrol: TPageControl;
    tabsheetPerformance: TTabSheet;
    tabsheetSettings: TTabSheet;
    editOldDriverLibrary: TEdit;
    editNewDriverLibrary: TEdit;
    labelOldDriverLibrary: TLabel;
    labelNewDriverLibrary: TLabel;
    buttonApply: TButton;
    listviewResults: TListView;
    comboTestType: TComboBoxEx;
    buttonRun: TButton;
    labelChooseTest: TLabel;
    labelConnectionParameters: TLabel;
    memoConnectionParameters: TMemo;
    labelTestResults: TLabel;
    buttonConfigure: TButton;
    groupboxTestDescription: TGroupBox;
    labelTestDecription: TLabel;
    labelIterationCount: TLabel;
    editIterationCount: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure buttonApplyClick(Sender: TObject);
    procedure comboTestTypeChange(Sender: TObject);
    procedure buttonRunClick(Sender: TObject);
    procedure buttonConfigureClick(Sender: TObject);
  private
    FCurrentTest: TAbstractTest;
    procedure ReceiveResults(OldResult, NewResult: Integer);
  end;

var
  FormMain: TFormMain;

implementation

uses Data;

{$R *.dfm}

procedure TFormMain.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to TestList.Count - 1 do
    comboTestType.AddItem(TAbstractTest(TestList[I]).Name, TestList[I]);
  try
    comboTestType.ItemIndex := IniFile.ReadInteger('general', 'testtype', 0);
  except
  end;
  comboTestTypeChange(comboTestType);
  editOldDriverLibrary.Text := DataMain.connectionOld.LibraryName;
  editNewDriverLibrary.Text := DataMain.connectionNew.LibraryName;
  memoConnectionParameters.Lines.Assign(DataMain.GetConnecionParams);
  editIterationCount.Text := IntToStr(DataMain.IterationCount);
end;

procedure TFormMain.FormDestroy(Sender: TObject);
begin
  IniFile.WriteInteger('general', 'testtype', comboTestType.ItemIndex);
end;

procedure TFormMain.buttonApplyClick(Sender: TObject);
begin
  DataMain.SetOldDriverLibrary(editOldDriverLibrary.Text);
  DataMain.SetNewDriverLibrary(editNewDriverLibrary.Text);
  DataMain.SetConnecionParams(memoConnectionParameters.Lines);
  DataMain.IterationCount := StrToInt(editIterationCount.Text);
end;

procedure TFormMain.comboTestTypeChange(Sender: TObject);
begin
  FCurrentTest := comboTestType.ItemsEx[comboTestType.ItemIndex].Data;
  FCurrentTest.OnReceiveResults := ReceiveResults;
  labelTestDecription.Caption := FCurrentTest.Description;
end;

procedure TFormMain.buttonRunClick(Sender: TObject);
var
  I: Integer;
begin
  listviewResults.Clear;
  Application.ProcessMessages;
  for I := 0 to DataMain.IterationCount - 1 do
    FCurrentTest.Run;
end;

procedure TFormMain.ReceiveResults(OldResult, NewResult: Integer);
begin
  with listviewResults.Items.Add do
  begin
    Caption := IntToStr(listviewResults.Items.Count);
    SubItems.Add(IntToStr(OldResult));
    SubItems.Add(IntToStr(NewResult));
    if OldResult > NewResult then
      SubItems.Add(FloatToStr(Round(10000*((OldResult - NewResult)/NewResult))/100))
    else
      SubItems.Add(FloatToStr(-Round(10000*((NewResult - OldResult)/OldResult))/100))
  end;
  Application.ProcessMessages;
end;

procedure TFormMain.buttonConfigureClick(Sender: TObject);
begin
  FCurrentTest.Configure;
end;

end.
